package com.meihua.shellapp.fragment;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.maker.MakerCenterActivity;
import com.meihua.shellapp.activity.order.MyOrderActivity;
import com.meihua.shellapp.activity.personalcenter.InvitationFriendActivity;
import com.meihua.shellapp.activity.personalcenter.MessageActivity;
import com.meihua.shellapp.activity.personalcenter.MyBrowseActivity;
import com.meihua.shellapp.activity.personalcenter.MyFollowActivity;
import com.meihua.shellapp.activity.personalcenter.MyReleaseActivity;
import com.meihua.shellapp.activity.personalcenter.PersonalInfoActivity;
import com.meihua.shellapp.activity.personalcenter.RewardMechanismActivity;
import com.meihua.shellapp.activity.set.SetUpActivity;
import com.meihua.shellapp.activity.wallet.MyWalletActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.StringBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.dialog.CheckPhotoDialog;
import com.meihua.shellapp.dialog.LoginandRegistrationDialog;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.PicassoUtils;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.io.File;

import butterknife.Bind;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class MeFragment extends BaseFragment {

    @Bind(R.id.tv_top_left)
    TextView tv_top_left;//头部左侧文字
    @Bind(R.id.tv_top_right)
    TextView tv_top_right;//头部右侧文字
    @Bind(R.id.ll_maker_center)
    LinearLayout ll_maker_center;//我的创客中心
    @Bind(R.id.ll_my_order)
    LinearLayout ll_my_order;//我的订单
    @Bind(R.id.ll_my_wallet)
    LinearLayout ll_my_wallet;//我的钱包
    @Bind(R.id.rl_personal_reward)
    RelativeLayout rl_personal_reward;//奖励机制
    @Bind(R.id.rl_personal_browse)
    RelativeLayout rl_personal_browse;//浏览记录
    @Bind(R.id.rl_personal_info)
    RelativeLayout rl_personal_info;//个人信息
    @Bind(R.id.rl_personal_follow)
    RelativeLayout rl_personal_follow;//我的关注
    @Bind(R.id.rl_personal_release)
    RelativeLayout rl_personal_release;//我的发布
    @Bind(R.id.rl_personal_invitation)
    RelativeLayout rl_personal_invitation;//邀请好友
    @Bind(R.id.sv_personalcenter)
    ScrollView sv_personalcenter;//包裹布局
    @Bind(R.id.iv_photo)
    ImageView iv_photo;//头像
    @Bind(R.id.tv_name)
    TextView tv_name;//名字

    private LoginandRegistrationDialog dialog;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_personalcenter;
    }

    @Override
    protected void initView() {
        sv_personalcenter.smoothScrollTo(0, 20);
        try{PicassoUtils.getInstance().bindImageData(iv_photo, MyApplication.getInstance().bean.getAvatar());}catch (Exception e){}
        //绑定数据后对listview进行定义高度
        tv_top_left.setVisibility(View.VISIBLE);
        tv_top_left.setText("设置");
        tv_top_right.setVisibility(View.VISIBLE);
        tv_top_right.setText("消息");
        tv_top_right.setOnClickListener(this);
        tv_top_left.setOnClickListener(this);
        ll_my_order.setOnClickListener(this);
        ll_my_wallet.setOnClickListener(this);
        ll_maker_center.setOnClickListener(this);
        rl_personal_reward.setOnClickListener(this);
        rl_personal_follow.setOnClickListener(this);
        rl_personal_info.setOnClickListener(this);
        rl_personal_release.setOnClickListener(this);
        rl_personal_browse.setOnClickListener(this);
        rl_personal_invitation.setOnClickListener(this);
        iv_photo.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isVisible() && MyApplication.getInstance().bean != null) {
            tv_name.setText(MyApplication.getInstance().bean.getName());
        }
    }

    @Override
    public void onClick(View view) {
        boolean isLogin = SharedPreferencesUtil.getInstance().getBoolean(getContext(), Constant.ISLOGIN,false);
        if (!isLogin){
            dialog = new LoginandRegistrationDialog.Builder(getContext())
                    .setClickSure(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getContext(), LoginActivity.class);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    })
                    .setClickCencle(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(getContext(), WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                            dialog.dismiss();
                        }
                    })
                    .setMessage("请您登录注册！")
                    .show();
            return;
        }
        Intent in = new Intent();
        switch (view.getId()) {
            case R.id.tv_top_right://消息
                in.setClass(getContext(), MessageActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_reward://奖励机制
                in.setClass(getContext(), RewardMechanismActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_browse://浏览记录
                in.setClass(getContext(), MyBrowseActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_info://个人信息
                in.setClass(getContext(), PersonalInfoActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_follow://我的关注
                in.setClass(getContext(), MyFollowActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_release://我的发布
                in.setClass(getContext(), MyReleaseActivity.class);
                startActivity(in);
                break;
            case R.id.rl_personal_invitation://邀请好友
                in.setClass(getContext(), InvitationFriendActivity.class);
                startActivity(in);
                break;
            case R.id.ll_my_order://我的订单
                in.setClass(getContext(), MyOrderActivity.class);
                startActivity(in);
                break;
            case R.id.ll_my_wallet://我的钱包
                in.setClass(getContext(), MyWalletActivity.class);
                startActivity(in);
                break;
            case R.id.ll_maker_center://创客中心
                in.setClass(getContext(), MakerCenterActivity.class);
                startActivity(in);
                break;
            case R.id.tv_top_left://设置
                in.setClass(getContext(), SetUpActivity.class);
                startActivity(in);
                break;
            case R.id.iv_photo://头像点击
                CheckPhotoDialog dialog = new CheckPhotoDialog(getActivity());
                dialog.show();
                break;
        }
    }

    /**
     * 上传头像
     *
     * @param url
     */
    public void setIvPhotoData(String url) {
        Luban.get(getContext())
                .load(new File(url))                     //传人要压缩的图片
                .putGear(Luban.THIRD_GEAR)      //设定压缩档次，默认三挡
                .setCompressListener(new OnCompressListener() { //设置回调
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onSuccess(File file) {
                        PicassoUtils.getInstance().bindImageDataAllPath(iv_photo, file);
                        OkGo.<String>post(URL.getUrl().UPDATEPHOTO)
                                .params("m_number", MyApplication.getInstance().getUserId())
                                .params("myFile1", file)
                                .execute(new MyStringCallBack<StringBean>(null) {
                                    @Override
                                    public void onSuccessData(StringBean bean) {
                                        super.onSuccessData(bean);
                                        if (bean.getCode() == 1) {
                                            MyApplication.getInstance().bean.setAvatar(bean.getData());
                                        }
                                        ToastUtils.getInstance().showToast(bean.getMsg());
                                    }
                                });
                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                }).launch();    //启动压缩
//        PicassoUtils.getInstance().bindImageDataAllPath(iv_photo, file);

    }
}
